﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;


namespace Business.Common
{
    public static class ComFunc
    {
        #region Json操作
        /// <summary>
        /// 引用类型转JSON
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static String T2Json<T>(this T t) where T : class
        {
            return t is string ? t as String : JsonConvert.SerializeObject(t);
        }
        /// <summary>
        /// JSON转引用类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static T Json2T<T>(this string t)
        {
            return JsonConvert.DeserializeObject<T>(t);
        }
        /// <summary>
        /// JSON转换为dynamic
        /// </summary>
        /// <param name="json">JSON格式字符串</param>
        /// <returns>动态类型</returns>
        public static dynamic Json2Dynamic(this String json)
        {
            return JsonConvert.DeserializeObject<ExpandoObject>(json);
        }
        /// <summary>
        /// dynamic转换JSON
        /// </summary>
        /// <param name="json">dynamic</param>
        /// <returns>动态类型</returns>
        public static string Dynamic2Json(dynamic json)
        {
            var Json = JsonConvert.SerializeObject(json);
            return Json == "null" ? null : Json;
        }
        #endregion

        #region 字符串转集合
        /// <summary>
        /// 数组拼接成字符串
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="splitStr"></param>
        /// <returns></returns>
        public static string ArrToStr(this string[] arr, char splitStr = ',')
        {
            if (arr == null || arr.Length == 0) return string.Empty;
            return arr.Aggregate(string.Empty, (current, t) => current + (t + splitStr)).TrimEnd(splitStr);
        }

        /// <summary>
        /// 字符串转化成数组
        /// </summary>
        /// <param name="str"></param>
        /// <param name="splitStr"></param>
        /// <returns></returns>
        public static string[] StrToArr(this string str, char splitStr = ',')
        {
            return string.IsNullOrEmpty(str) ? new string[] { } : str.Split(splitStr).ToArray();
        }

        /// <summary>
        /// 字符串转化成List集合
        /// </summary>
        /// <param name="str"></param>
        /// <param name="splitStr"></param>
        /// <returns></returns>
        public static List<string> StrToList(this string str, char splitStr = ',')
        {
            return string.IsNullOrEmpty(str) ? new List<string>() : str.Split(splitStr).ToList();
        } 
        #endregion

        #region  枚举操作

        public static T StrToEnum<T>(this string EnumStr)
        {
            return (T)(System.Enum.Parse(typeof(T), EnumStr));
        }
        /// <summary>
        /// 将枚举转化成枚举int值的字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_enum"></param>
        /// <returns></returns>
        public static string EnumToIntStr<T>(this T _enum)
        {
            return _enum.GetHashCode().ToString();
        }

        /// <summary>
        /// 枚举值转换为Value类型后进行比较
        /// </summary>
        /// <typeparam name="T">待比较值(String,Int16,Int32,UInt16,UInt32)</typeparam>
        /// <param name="t"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Boolean EnumCompare<T>(this System.Enum t, T value) where T : class
        {
            var type = t.GetType();
            try
            {
                var enumValue = System.Enum.Parse(type, value.ToString());
                return System.Enum.IsDefined(type, enumValue) && t.Equals(enumValue);
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 通过枚举字符值获取枚举属性Attribute
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string GetEnumDescription<TEnum>(string enumValue) where TEnum : struct
        {
            var fie = typeof(TEnum).GetField(enumValue);
            var description = fie.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute;
            return description == null ? "" : description.Description;
        }
        #endregion

        #region  加密
        /// <summary>
        /// SHA1加密字符串
        /// 默认Utf-8字符集
        /// </summary>
        /// <param name="strOrgData">源文</param>
        /// <returns></returns>
        public static string Sha1Sign(string strOrgData)
        {
            var hashSha1 = new SHA1CryptoServiceProvider();
            var bytValue = Encoding.GetEncoding("UTF-8").GetBytes(strOrgData);
            var bytHash = hashSha1.ComputeHash(bytValue);
            hashSha1.Clear();
            return BitConverter.ToString(bytHash).Replace("-", String.Empty);
        }
        #endregion
    }
}
